<?php
	class Model_Notification extends Joob_Db_Table	{
		
		protected $key = "id";
	
		protected $db = "notifications";
		
        public $id;
        public $user_id;
        public $target_id;
        public $type;
        public $content;
        public $since;
        
		public function add(){
			$this->since = time();
        	return $this->insert('user_id, target_id, type, content, since');
        }
        
        public function getNewNotifications($userID, $since)	{
        	return $this->search("since > $since AND user_id = $userID", "*");
        }
        
        public function getOldNotifications($userID, $since, $page=0, $page_size=15)	{
        	return $this->search("since <= $since AND user_id = $userID", "*", "$page, $page_size");
        }
        
        public function countNewNotifications($userID, $since)	{
        	$rs = $this->search("since > $since AND user_id = $userID", "count(id) as count");
        	if (empty($rs) || is_null($rs))	{
        		return 0;
        	}
        	return $rs[0]->count;
        }
	}
?>